package com.codeoss.framework.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.response.ViewResponse;
import com.codeoss.framework.service.IViewService;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.component.store.IComponentStore;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.renderer.IComponentRenderer;
import com.codeoss.framework.view.renderer.ViewPropMap;
import com.codeoss.framework.view.renderer.ViewSerializeMap;
import com.codeoss.framework.view.renderer.store.IComponentRendererStore;
import com.codeoss.framework.view.service.IComponentService;
import com.codeoss.framework.view.service.IPageComponentService;
import com.codeoss.framework.view.service.store.IComponentServiceStore;

@Service
public class ViewService implements IViewService ,ApplicationContextAware{
	@Autowired
	private IComponentStore componentStore;
	@Autowired
	private IComponentServiceStore componentServiceStore;
	@Autowired
	private IComponentRendererStore componentRendererStore;
    private ApplicationContext applicationContext;
	@Override
	@Transactional
	public ViewResponse initPage(YunContext yunContext, String code) {
		// TODO Auto-generated method stub
		ViewResponse viewResponse = new ViewResponse();
		yunContext.setPostBack(false);
		Page page = componentStore.get(code);
		yunContext.setPage(page);
		IPageComponentService pageComponentService = componentServiceStore.getComponentService(yunContext, page);
		pageComponentService.init(yunContext, page);
		
		IComponentRenderer componentRenderer=componentRendererStore.getComponentRenderer(yunContext,page);
		ViewSerializeMap viewSerializeMap=componentRenderer.renderView(yunContext, page);
		viewResponse.setViewSerializeMap(viewSerializeMap);
		if(!yunContext.isRenderCompleted()) {
			ViewPropMap viewPropMap=new ViewPropMap();
			componentRenderer.renderProp(yunContext, page, viewPropMap);
			viewResponse.setViewPropMap(viewPropMap);
		}
		
		viewResponse.setClientScriptList(yunContext.getClientScriptList());
		viewResponse.setMessageList(yunContext.getMessageList());
		return viewResponse;
	}

	@Override
	@Transactional
	public ViewResponse processRequest(YunContext yunContext, String code) {
		// TODO Auto-generated method stub
		ViewResponse viewResponse = new ViewResponse();
		yunContext.setPostBack(true);
		Page page = componentStore.get(code);
		yunContext.setPage(page);
		IPageComponentService pageComponentService = componentServiceStore.getComponentService(yunContext, page);
		pageComponentService.processRequest(yunContext, page);
		
		String eventSource=yunContext.getViewRequest().getEventSource();
		String eventType=yunContext.getViewRequest().getEventType();
		if(!StringUtils.isEmpty(eventSource)){
			if("componentValueChangeSystemEvent".equals(eventType)) {
				BasePageBean basePageBean=(BasePageBean)applicationContext.getBean(page.getPageBean());
				basePageBean.processComponentValueChangeSystemEvent();
			}else {
				page.findComponentById(yunContext,eventSource);
				IComponentService componentService =(IComponentService)componentServiceStore.getComponentService(yunContext,yunContext.getFindComponent());
				componentService.processEvent(yunContext, yunContext.getFindComponent(),eventType);
			}
		}
		
		
		if(!yunContext.isRenderCompleted()) {
			List<Component> renderComponentList=yunContext.getRenderComponentList();
			ViewPropMap viewPropMap=new ViewPropMap();
			viewResponse.setViewPropMap(viewPropMap);
			if(renderComponentList!=null&&renderComponentList.size()>0) {
				for(int i=0;i<renderComponentList.size();i++) {
					IComponentRenderer componentRenderer=componentRendererStore.getComponentRenderer(yunContext,renderComponentList.get(i));
					componentRenderer.renderProp(yunContext, renderComponentList.get(i), viewPropMap);
				}
			}else {
				IComponentRenderer componentRenderer=componentRendererStore.getComponentRenderer(yunContext,page);
				componentRenderer.renderProp(yunContext, page, viewPropMap);
			}
		}
		
		viewResponse.setClientScriptList(yunContext.getClientScriptList());
		viewResponse.setMessageList(yunContext.getMessageList());
		return viewResponse;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

}
